import { request } from '../../utils/request.js'
Page({
    data: {
        name: '',
        tel: '',
        area: '',
        detail: '',
        region: ['云南省', '昆明市', '呈贡区'], //地区选择区打开后默认选中项
    },
    changeArea(event) {
        const { value } = event.detail
        this.setData({ area: value.join(' ') })
    },
    onLoad: function(options) {
        let { edit, name, tel, a, b, c, d } = options
        if (edit === 'true') {
            wx.setNavigationBarTitle({
                title: '修改收货地址'
            })
            return this.setData({ name, tel, area: `${a} ${b} ${c}`, region: [a, b, c], detail: d, isedit: true })
        }
        wx.setNavigationBarTitle({
            title: '添加收货地址'
        })
    },
    edit(e) {
        let { name, tel, area, detail } = this.data
        area = area.split(' ').join('')
        if (!name) {
            return wx.showToast({
                title: '请输入您的姓名',
                icon: 'none'
            })
        }
        if (!tel) {
            return wx.showToast({
                title: '填输入您的手机号',
                icon: 'none'
            })
        }
        if (tel) {
            const reg = /^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$/
            if (!reg.test(tel)) {
                return wx.showToast({
                    title: '请输入正确的手机号码',
                    icon: 'none'
                })
            }
        }
        if (!area) {
            return wx.showToast({
                title: '请选择您的地区',
                icon: 'none'
            })
        }
        if (!detail) {
            return wx.showToast({
                title: '请填写您的详细地址',
                icon: 'none'
            })
        }
        const { sign } = e.target.dataset
        if (sign === '1') {
            return request({
                url: '/home/user/addr/add',
                data: {
                    contact: name,
                    mobile: tel,
                    addr: area,
                    house: detail
                },
                success(data) {
                    console.log(data)
                }
            })
        }
    }
})